package com.ksyun.campus.metaserver.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * DataServer information
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DataServerInfo {
    
    private String dsId;
    private String host;
    private int port;
    private long capacityBytes;
    private long usedBytes;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastSeen;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastHeartbeat;
    
    private Map<String, String> labels;
    private DataServerStatus status;
    
    public enum DataServerStatus {
        ACTIVE, INACTIVE, DECOMMISSIONED
    }
    
    public double getUsageRatio() {
        if (capacityBytes == 0) {
            return 0.0;
        }
        return (double) usedBytes / capacityBytes;
    }
    
    public long getAvailableBytes() {
        return capacityBytes - usedBytes;
    }
    
    public boolean isHealthy(int timeoutSeconds) {
        if (lastSeen == null) {
            return false;
        }
        return lastSeen.isAfter(LocalDateTime.now().minusSeconds(timeoutSeconds));
    }
}
